package com.yunduansing.demo.web.system;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunduansing.demo.entity.RoleEntity;
import com.yunduansing.demo.exception.RecordExistsException;
import com.yunduansing.demo.model.common.PageRequestModel;
import com.yunduansing.demo.model.common.PageResultModel;
import com.yunduansing.demo.model.common.ResponseModel;
import com.yunduansing.demo.service.RoleService;
import com.yunduansing.demo.web.BaseController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "角色相关")
@RestController
public class RoleController extends BaseController {
    @Autowired
    private RoleService service;

    @GetMapping("/role/page")
    @ApiOperation("分页")
    public ResponseModel<PageResultModel<RoleEntity>> getPage(PageRequestModel model) {
        QueryWrapper<RoleEntity> wrapper=new QueryWrapper<RoleEntity>();
        if(!StringUtils.isEmpty(model.getKey())){
            wrapper.or().like("name", model.getKey()).or().like("code", model.getKey());
        }
        
        Page<RoleEntity> page=new Page<>(model.getCurrent(),model.getPageSize());
        var data=service.page(page, wrapper);
        return ok(PageResultModel.page(data.getTotal(), data.getRecords()));
    }

    @PostMapping("/role/create")
    @ApiOperation("创建")
    public ResponseModel create(@RequestBody RoleEntity model) throws RecordExistsException {
        var queryWrapper=new QueryWrapper<RoleEntity>();
        queryWrapper.like("code", model.getCode()).or().like("name", model.getName());
        var exists=service.list(queryWrapper);
        if(exists.stream().filter(x->x.getCode().equals(model.getCode())).count()>0) throw new RecordExistsException("已存在code为："+model.getCode()+"的角色。");
        if(exists.stream().filter(x->x.getName().equals(model.getName())).count()>0) throw new RecordExistsException("已存在名为为："+model.getName()+"的角色。");
        return convertToResponseModel(service.save(model), "创建失败。");
    }

    @PostMapping("/role/update")
    @ApiOperation("修改")
    public ResponseModel update(@RequestBody RoleEntity model) throws RecordExistsException {
        var queryWrapper=new QueryWrapper<RoleEntity>();
        queryWrapper.like("code", model.getCode()).or().like("name", model.getName());
        var exists=service.list(queryWrapper);
        if(exists.stream().filter(x->x.getCode().equals(model.getCode())&&x.getId()!=model.getId()).count()>0) throw new RecordExistsException("已存在code为："+model.getCode()+"的角色。");
        if(exists.stream().filter(x->x.getName().equals(model.getName())&&x.getId()!=model.getId()).count()>0) throw new RecordExistsException("已存在名为为："+model.getName()+"的角色。");
        return convertToResponseModel(service.updateById(model), "修改失败。");
    }

    @PostMapping("/role/delete")
    @ApiOperation("删除")
    public ResponseModel delete(@RequestBody List<Integer> ids){
        return convertToResponseModel(service.removeByIds(ids), "删除失败");
    }

    @GetMapping("/role/list/user")
    @ApiOperation("根据user查询")
    public ResponseModel<List<RoleEntity>> getListByUser(int userId){
        return ok(service.getListByUser(userId));
    }

    @GetMapping("/role/list")
    @ApiOperation("获取所有数据")
    public ResponseModel<List<RoleEntity>> getList(){
        return ok(service.list());
    }
}